package org.chat.client.screens;


import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Le panel Smielys
 * @author AGRAM Larbi
 *@version 1.1
 */
public class PanelSmielys extends PopupPanel {


	public interface Images extends ClientBundle {

		/**
		 * @ImageBundle cry.gif
		 */
		@Source("org/chat/client/images/smileys/cry.gif")
		ImageResource cry();

		/**
		 * @ImageBundle cool.gif
		 */
		@Source("org/chat/client/images/smileys/cool.gif")
		ImageResource cool();

		/**
		 * @ImageBundle embarassed.gif
		 */
		@Source("org/chat/client/images/smileys/embarassed.gif")
		ImageResource embarassed();

		/**
		 * @ImageBundle frown.gif
		 */
		@Source("org/chat/client/images/smileys/frown.gif")
		ImageResource frown();

		/**
		 * @ImageBundle innocent.gif
		 */
		@Source("org/chat/client/images/smileys/innocent.gif")
		ImageResource innocent();

		/**
		 * @ImageBundle kiss.gif
		 */
		@Source("org/chat/client/images/smileys/kiss.gif")
		ImageResource kiss();

		/**
		 * @ImageBundle laughing.gif
		 */
		@Source("org/chat/client/images/smileys/laughing.gif")
		ImageResource laughing();

		/**
		 * @ImageBundle money-mouth.gif
		 */
		@Source("org/chat/client/images/smileys/money-mouth.gif")
		ImageResource moneymouth();

		/**
		 * @ImageBundle sealed.gif
		 */
		@Source("org/chat/client/images/smileys/sealed.gif")
		ImageResource sealed();



		/**
		 * @ImageBundle smile.gif
		 */
		@Source("org/chat/client/images/smileys/smile.gif")
		ImageResource smile();

		/**
		 * @ImageBundle surprised.gif
		 */
		@Source("org/chat/client/images/smileys/surprised.gif")
		ImageResource surprised();

		/**
		 * @ImageBundle tongue-out.gif
		 */
		@Source("org/chat/client/images/smileys/tongue-out.gif")
		ImageResource tongueout();

		/**
		 * @ImageBundle undecided.gif
		 */
		@Source("org/chat/client/images/smileys/undecided.gif")
		ImageResource undecided();

		/**
		 * @ImageBundle wink.gif
		 */
		@Source("org/chat/client/images/smileys/wink.gif")
		ImageResource wink();

		/**
		 * @ImageBundle yell.gif
		 */
		@Source("org/chat/client/images/smileys/yell.gif")
		ImageResource yell();


	}

	private class EventListener implements ClickHandler, ChangeHandler,
	KeyUpHandler {

		public void onChange(ChangeEvent event) {

		}

		public void onClick(ClickEvent event) {

			Widget sender = (Widget) event.getSource();

			if (sender == cry) {
				richText.setHTML(richText.getHTML()+"<img src=\""+images.cry().getURL()+"\" />");
				hide();
			} else if (sender == cool) {
				richText.setHTML(richText.getHTML()+"<img src=\""+images.cool().getURL()+"\" />");
				hide();
			} else if (sender == embarassed) {
				richText.setHTML(richText.getHTML()+"<img src=\""+images.embarassed().getURL()+"\" />");
				hide();						
			} else if (sender == frown) {
				richText.setHTML(richText.getHTML()+"<img src=\""+images.frown().getURL()+"\" />");
				hide();
			} else if (sender == innocent) {
				richText.setHTML(richText.getHTML()+"<img src=\""+images.innocent().getURL()+"\" />");
				hide();
			} else if (sender == kiss) {
				richText.setHTML(richText.getHTML()+"<img src=\""+images.kiss().getURL()+"\" />");
				hide();
			} else if (sender == laughting) {
				richText.setHTML(richText.getHTML()+"<img src=\""+images.laughing().getURL()+"\" />");
				hide();
			} else if (sender == moneymouth) {
				richText.setHTML(richText.getHTML()+"<img src=\""+images.moneymouth().getURL()+"\" />");
				hide();
			} else if (sender == sealed) {
				richText.setHTML(richText.getHTML()+"<img src=\""+images.sealed().getURL()+"\" />");
				hide();
			} else if (sender == smile) {
				richText.setHTML(richText.getHTML()+"<img src=\""+images.smile().getURL()+"\" />");
				hide();
			} else if (sender == surprised) {
				richText.setHTML(richText.getHTML()+"<img src=\""+images.surprised().getURL()+"\" />");
				hide();
			} else if (sender == tongueout) {
				richText.setHTML(richText.getHTML()+"<img src=\""+images.tongueout().getURL()+"\" />");
				hide();
			} else if (sender == undecided) {
				richText.setHTML(richText.getHTML()+"<img src=\""+images.undecided().getURL()+"\" />");
				hide();
			} else if (sender == wink) {
				richText.setHTML(richText.getHTML()+"<img src=\""+images.wink().getURL()+"\" />");
				hide();
			} else if (sender == yell) {
				richText.setHTML(richText.getHTML()+"<img src=\""+images.yell().getURL()+"\" />");
				hide();
			} 
		}

		public void onKeyUp(KeyUpEvent event) {

			Widget sender = (Widget) event.getSource();

			if (sender == richText) {

			}
		}
	}
	
	private Images images = (Images) GWT.create(Images.class);
	private EventListener listener = new EventListener();

	private RichTextArea richText;

	private VerticalPanel outer = new VerticalPanel();
	private HorizontalPanel topPanel = new HorizontalPanel();

	private PushButton cry;
	private PushButton cool;
	private PushButton embarassed;
	private PushButton frown;
	private PushButton innocent;
	private PushButton kiss;
	private PushButton laughting;
	private PushButton moneymouth;
	private PushButton sealed;
	private PushButton smile;
	private PushButton surprised;
	private PushButton tongueout;
	private PushButton undecided;
	private PushButton wink;
	private PushButton yell;
	
	Button closeButton = new Button("Fermer");

	public PanelSmielys(RichTextArea richText) {
		this.richText = richText;
		//basic = richText.getFormatter();
		//extended = richText.getFormatter();

		outer.add(topPanel);
		
		outer.add(closeButton);
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				hide();

			}
		});

		topPanel.setWidth("50%");
		topPanel.setWidth("40%");

		setWidget(outer);
		setStyleName("gwt-Smielysbar");


		topPanel.add(cry = createPushButton(images.cry(), " ;( "));
		topPanel.add(cool = createPushButton(images.cool()," lol "));
		topPanel.add(embarassed = createPushButton(images.embarassed(), " :| "));
		topPanel.add(frown = createPushButton(images.frown(), " :( "));
		topPanel.add(innocent = createPushButton(images.innocent(), " :. "));
		topPanel.add(kiss = createPushButton(images.kiss(), " :h "));
		topPanel.add(laughting = createPushButton(images.laughing(), " :D "));
		topPanel.add(moneymouth = createPushButton(images.moneymouth(), " :$ "));
		topPanel.add(sealed = createPushButton(images.sealed(), " :l "));
		topPanel.add(smile = createPushButton(images.smile(), " :) "));
		topPanel.add(surprised = createPushButton(images.surprised(), " :o "));
		topPanel.add(tongueout = createPushButton(images.tongueout(), " :p "));
		topPanel.add(undecided = createPushButton(images.undecided(), " :v "));
		topPanel.add(wink = createPushButton(images.wink(), " ;) "));
		topPanel.add(yell = createPushButton(images.yell(), " xo "));


		richText.addKeyUpHandler(listener);
		richText.addClickHandler(listener);
	}

	/**
	 * 
	 * @param img l'image 
	 * @param tip le titre
	 * @return un widget ici c'est un PushButton
	 */
	private PushButton createPushButton(ImageResource img, String tip) {
		PushButton pb = new PushButton(new Image(img));
		pb.addClickHandler(listener);
		pb.setTitle(tip);
		return pb;
	}


}
